<template>
    <view class="container">
        <image class="bg-image" src="/static/image.jpg" mode="aspectFill"></image>
        <view class="nav-bar">
            <view class="back-btn" @click="goBack">
                <image src="/static/return.png" mode="aspectFit" class="back-icon"/>
            </view>
            <text class="nav-title">词典列表</text>
        </view>
        
        <!-- 搜索框 -->
        <view class="search-container">
            <view class="search-box">
                <image src="/static/book/search.png" class="search-icon" mode="aspectFit"/>
                <input
                    type="text"
                    v-model="searchText"
                    placeholder="搜索词典"
                    placeholder-class="placeholder"
                    class="search-input"
                    @confirm="handleSearch"
                />
                <image 
                    v-if="searchText"
                    src="/static/clear.png" 
                    class="clear-icon" 
                    mode="aspectFit"
                    @click="clearSearch"
                />
            </view>
        </view>

        <view class="content">
            <view class="card-body">
                <view 
                    class="dictionary-item" 
                    v-for="item in dictionaryList" 
                    :key="item.dictionary_id"
                    @click="selectDictionary(item)"
                >
                    <image 
                        :src="'/static/book/book' + (Math.floor(Math.random() * 2) + 3) + '.jpg'" 
                        class="book-cover" 
                        mode="aspectFill"
                    />
                    <view class="dictionary-info">
                        <text class="dictionary-name">{{ item.dictionary_name }}</text>
                        <text class="dictionary-date">{{ formatDate(item.create_time) }}</text>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import { dictionary } from '@/api/user.js'

export default {
    data() {
        return {
            searchText: '',
            dictionaryList: []
        }
    },
    onShow() {
        this.loadDictionaries()
    },
    methods: {
        goBack() {
            uni.navigateBack()
        },

        async loadDictionaries() {
            try {
                const res = await dictionary()
                console.log('获取词典列表响应：', res)
                
                if (res.data.code =2000) {
                    this.dictionaryList = res.data
                    // 保存词典列表到本地存储
                    uni.setStorageSync('dictionaryList', this.dictionaryList)
                } else {
                    console.error('获取词典列表失败：', res)
                    uni.showToast({
                        title: res.msg || '获取词典列表失败',
                        icon: 'none'
                    })
                }
            } catch (error) {
                console.error('加载词典失败：', error)
                uni.showToast({
                    title: '加载词典失败，请重试',
                    icon: 'none'
                })
            }
        },

        handleSearch() {
            if (!this.searchText) {
                this.loadDictionaries()
                return
            }
            
            const filteredList = this.dictionaryList.filter(dict => 
                dict.dictionary_name.toLowerCase().includes(this.searchText.toLowerCase())
            )
            this.dictionaryList = filteredList
        },

        clearSearch() {
            this.searchText = ''
            this.loadDictionaries()
        },

        selectDictionary(item) {
            // 保存选中的词典信息到本地存储
            try {
                uni.setStorageSync('selectedDictionary', {
                    id: item.dictionary_id,
                    name: item.dictionary_name,
                    time: item.create_time
                })
                
                // 跳转到计划页面
                uni.navigateTo({
                    url: `/pages/home/plan?id=${item.dictionary_id}&name=${item.dictionary_name}`
                })
            } catch (error) {
                console.error('保存词典信息失败:', error)
                uni.showToast({
                    title: '操作失败，请重试',
                    icon: 'none'
                })
            }
        },

        formatDate(dateString) {
            if (!dateString) return ''
            const date = new Date(dateString)
            return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
        }
    }
}
</script>

<style>
.container {
    position: relative;
    min-height: 100vh;
}

.bg-image {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -1;
}

.nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    padding: 40px 20px 20px;
    z-index: 100;
}

.back-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.back-icon {
    width: 24px;
    height: 24px;
}

.nav-title {
    flex: 1;
    text-align: center;
    font-size: 18px;
    font-weight: 500;
    color: #333;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
}

/* 搜索框样式 */
.search-container {
	margin-top: 100px;
    padding: 10px 20px 0;  /* 减小顶部padding */
}

.search-box {
    position: relative;
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    padding: 8px 15px;
    margin-bottom: 20px;
}

.search-icon {
    width: 40px;
    height: 40px;
    margin-right: 8px;
}

.search-input {
    flex: 1;
    height: 36px;
    font-size: 14px;
    color: #333;
    background: transparent;
}

.clear-icon {
    width: 20px;
    height: 20px;
    padding: 2px;
}

.placeholder {
    color: #999;
}

/* 内容区域样式 */
.content {
    padding: 10px 20px;  /* 减小顶部padding */
    min-height: 100vh;
}

.card-body {
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 20px;
    //margin-top: 20px;
}

.info-item {
    width: 100%;
    height: 44px;
    border: 1px solid rgba(238, 238, 238, 0.5);
    border-radius: 8px;
    margin-bottom: 15px;
    padding: 0 15px;
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.1);
}

.item-label {
    width: 60px;
    color: #333;
    font-size: 14px;
}

.item-value {
    flex: 1;
    color: #333;
    font-size: 14px;
    margin-left: 10px;
}

/* 标签切换样式 */
.tab-container {
    display: flex;
    justify-content: space-around;
    padding: 0 20px;
    margin-top: 10px;  /* 调整位置，避免被导航栏遮挡 */
    margin-bottom: 10px;
}

.tab-item {
    flex: 1;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    margin: 0 10px;
    border-radius: 20px;
    transition: all 0.3s ease;
}

.tab-item.active {
    background: rgba(107, 75, 138, 0.9);
}

.tab-text {
    font-size: 14px;
    color: #333;
}

.active .tab-text {
    color: #fff;
}

.dictionary-item {
    display: flex;
    align-items: center;
    padding: 15px;
    margin-bottom: 15px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    transition: all 0.3s ease;
}

.dictionary-item:active {
    background: rgba(255, 255, 255, 0.2);
}

.book-cover {
    width: 60px;
    height: 80px;
    border-radius: 4px;
    margin-right: 15px;
}

.dictionary-info {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.dictionary-name {
    font-size: 16px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 500;
}

.dictionary-date {
    font-size: 12px;
    color: #666;
}

.empty-tip {
    text-align: center;
    color: #666;
    padding: 20px;
}
</style>